package com.longtop.perform.zb.system.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.common.QueryConditions;
import com.longtop.framework.common.QueryResult;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;
import com.longtop.perform.zb.index.service.IndexService;
import com.longtop.perform.zb.system.model.TZbSystemClass;
import com.longtop.perform.zb.system.service.SystemClassService;

/**
 * 
 * @author 杨向阳
 * 
 */
public class SystemClassEditAction extends BaseAction {

	private final static String DATASET_SYSTEMCLASS = "Dateset_SystemClass";
	private final static String DATASET_SYSTEMCLASSINDEX = "Dateset_SystemClassIndex";

	/**
	 * 系统共用：初始化调用
	 */
	public void init(IInitContext context) {
		super.init(context);
	}

	/**
	 * 本页jsp：初始化业务数据(覆写父类方法)
	 */
	public void thisPageInit(IInitContext context) {

		String systemId = context.getRequest().getParameter("systemId");
		String classId = context.getRequest().getParameter("classId");
		String parentClassId = context.getRequest().getParameter("parentClassId");
		// 调用Spring的业务逻辑类

		Dataset systemClassDataset = (Dataset) context.getDataset(DATASET_SYSTEMCLASS);
		Dataset systemClassIndexDataset = (Dataset) context.getDataset(DATASET_SYSTEMCLASSINDEX);
		// 获取页面上的dataset的结构
		if (classId == null || "".equals(classId)) {
			TZbSystemClass zbSystemClass = new TZbSystemClass();
			zbSystemClass.setParentClassId(parentClassId);
			zbSystemClass.setSystemId(systemId);
			List list = new ArrayList();
			list.add(zbSystemClass);
			BeanUtil.listToDataset(list, systemClassDataset, false);
			systemClassDataset.getCurrentRecord().setState(1);

		} else {
			// 验证数据集对象是否存在
			if (!WebUtil.validateDatasetExist(this, systemClassDataset, DATASET_SYSTEMCLASS)) {
				return;
			}
			// 设置定性
			SystemClassService systemClassService = getBean(SystemClassService.BEAN_ID);
			QueryConditions classQueryConditions = WebUtil.createQueryConditions();
			classQueryConditions.setCondition("classId", QueryConditions.EQUAL, classId);

			QueryResult classQueryResult = systemClassService.loadSystemClass(classQueryConditions, 1, -1);
			IndexService zbIndexService = getBean(IndexService.BEAN_ID);

			HashMap hashMap = new HashMap();
			hashMap.put("classId", classId);

			List list = zbIndexService.loadIndexNoPage(hashMap);
			try {
				BeanUtil.queryResultToDataset(classQueryResult, systemClassDataset, false, true);
				BeanUtil.listToDataset(list, systemClassIndexDataset, false);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @param context
	 */
	public void saveSystemClass(IActionContext context) {

		// 获取页面上的dataset的结构
		Dataset dataset = (Dataset) context.getDataset(DATASET_SYSTEMCLASS);
		Dataset zbSystemClassIndexDataset = (Dataset) context.getDataset(DATASET_SYSTEMCLASSINDEX);

		// 验证数据集是否存在及是否有变化
		if (!WebUtil.validateDatasetChange(this, dataset, DATASET_SYSTEMCLASS)) {
			return;
		}

		// 当前SessionObject对象
		SessionObject sessionObject = this.getSessionObject(context);

		// 调用Spring的业务逻辑类
		SystemClassService systemClassService = getBean(SystemClassService.BEAN_ID);

		try {
			// 把dataset转成要删除的主键列表、新增bean及更新bean列表

			Map objectList = WebUtil.datasetToObjectList(dataset, "classId", TZbSystemClass.class, "");
			List systemClassIndexList = zbSystemClassIndexDataset.getRecordList();
			// 调用业务逻辑层的批量更新方法(在一个事务内实现)
			Integer[] count = systemClassService.saveSystemClass(objectList, sessionObject, systemClassIndexList);

			// 向JSP页面传递操作结果信息
			this.hintInfoMessage(WebUtil.getSubmitSuccessInfo(count[0].intValue(), count[1].intValue(), count[2].intValue()));
		} catch (Exception e) {
			// 记入日志
			log.logError(e.getMessage());
			// 在界面显示错误信息
			this.hintErrorMessage(e.getMessage());
		}
	}
}